<?php
class PNUserHotelAllotment extends PNObject {
  function PNUserHotelAllotment($init=null, $where='')
  {
    $this->PNObject();

    $this->_objType       = 'hotelbooking_hotelallotment';
    $this->_objField      = 'id';
    $this->_objPath       = 'form';
    
    $this->_objJoin[] = array ( 'join_table' => 'hotelbooking_hotelmember',
                                'compare_field_table' => 'hotelMember_id',
                                'compare_field_join' => 'id',
                                'join_field' => array('id', 'hotelName','description','accommodation','facilities','restaurant','contactus','other','latitude','longitude'),
                                'object_field_name' => array('member_id', 'member_hotelName', 'member_description', 'member_accommodation','member_facilities','member_restaurant','member_contactus','member_other','member_latitude','member_longitude')
                        );

    $this->_init($init, $where);
  }
  
  function genFilter(){
    $uid = SessionUtil::getVar('uid');
    $id = FormUtil::getPassedValue("id",FALSE);

    if($id){
      $wheres[] = $this->_columns['id']." = ".$id;
    }
    if($uid){
      $wheres[] = $this->_columns['cr_uid']." = ".$uid;
    }
    if(count($wheres)>1){
      $where = implode(" AND ",$wheres);
    }else{
      $where = $wheres[0];
    }
    
    return $where;
  }
  function genForward()
  {
    return pnModUrl("hotelbooking","user","listallotment");
  }
  
  function save()
  {
    
    $status = FormUtil::getPassedValue("status",FALSE,"POST");
    $this->_prepareData();
    $rc = true;
    if ($status == "edit") {
        $rc = $this->update();
    } else if ($status == "new"){
        $rc = $this->insert();
    }
    return $rc;
  }
  
  function insert()
  {
    return DBUtil::insertObjectArray($this->_objData,$this->_objType);
  }
  
  function update()
  {
    return DBUtil::updateObjectArray($this->_objData,$this->_objType);
  }
  
  function _prepareData()
  {
    $status = FormUtil::getPassedValue("status",FALSE,"POST");
    
    if (!($class = Loader::loadClassFromModule ('HotelBooking',"UserHotelmember", false)))
        return LogUtil::registerError ("Unable to load class [UserHotelmember] ...");
        
    $object  = new $class ();
    $where = $object->_columns['cr_uid']." = ".SessionUtil::getVar('uid');
    $object->getWhere($where);
    $hotelID = $object->_objData[id];
    
    if(!isset($hotelID)){
      return LogUtil::registerError ("Unable to load hotel description.");
    }
    
    if($status == "new"){
      $i=0;
      foreach($this->_objData AS $roomId=>$roomValue){
        foreach($roomValue AS $year=>$yearValue){
          foreach($yearValue AS $month=>$monthValue){
            foreach($monthValue AS $day=>$datAllotment){
              $data[$i]['date'] = "$year-$month-$day";
              $data[$i]['available'] = $datAllotment;
              $data[$i]['hotelRoom_id'] = $roomId;
              $data[$i]['hotelMember_id'] = $hotelID;
              $i++;
            }
          }
        }
      }
      $this->_objData = $data;
    }else if($status == "edit"){
      //edit
      $i=0;
      foreach($this->_objData AS $roomId=>$roomValue){
        foreach($roomValue AS $year=>$yearValue){
          foreach($yearValue AS $month=>$monthValue){
            foreach($monthValue AS $day=>$dataId){
              foreach($dataId AS $idKey=>$dataAllotment){
                $data[$i]['id'] = $idKey;
                $data[$i]['date'] = "$year-$month-$day";
                $data[$i]['available'] = $dataAllotment;
                $data[$i]['hotelRoom_id'] = $roomId;
                $data[$i]['hotelMember_id'] = $hotelID;
              }
              $i++;
            }
          }
        }
      }
      $this->_objData = $data;
      //var_dump($this->_objData);exit;
    }

  }
  
}